<template>
  <!-- 添加或修改工艺管理加工任务 对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
    <el-form
      ref="form"
      :inline="true"
      layout="inline"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="任务名称" prop="workingName">
        <el-input v-model="form.workingName" placeholder="请输入任务名称" />
      </el-form-item>
      <el-form-item label="设备" prop="equipmentId">
        <el-select
          v-model="form.equipmentId"
          placeholder="请选择设备"
          clearable
          size="small"
        >
          <el-option
            v-for="item in equipmentOptions"
            :key="item.equipmentId"
            :label="item.equipmentName"
            :value="item.equipmentId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="公司" prop="deptId">
        <el-select
          v-model="form.deptId"
          placeholder="请选择公司"
          clearable
          size="small"
        >
          <el-option
            v-for="item in deptOptions"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务描述" prop="description">
        <el-input v-model="form.description" placeholder="请输入任务描述" />
      </el-form-item>

      <el-form-item label="增材方式" prop="technologyMaterialAddMethodId">
        <el-select
          v-model="form.technologyMaterialAddMethodId"
          placeholder="请选择增材方式"
          clearable
          size="small"
        >
          <el-option
                v-for="dict in materialAddMethodOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              >
               <span style="float: left">{{ dict.dictDataAbbreviation }}</span>
      <span style="float: right; color: #8492a6; font-size: 13px">{{ dict.dictLabel}}</span>
              </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="平均电价" prop="electricityPrice">
        <el-input
          v-model="form.electricityPrice"
          placeholder="请输入平均电价"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          type="textarea"
          v-model="form.remark"
          placeholder="请输入备注"
        />
      </el-form-item>
      <model-material ref="materialRef" :setSelectItem="setSelectItems" />
      <div style="margin-top: 10px">
        <process-parameters
          ref="parametersRef"
          :id="form.equipmentId"
          :selectItems="selectItems"
        />
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  getWorking,
  addWorking,
  updateWorking,
} from "@/api/technology/working";
import ModelMaterial from "./ModelMaterial.vue";
import ProcessParameters from "./ProcessParameters.vue";
export default {
  props: ["equipmentOptions", "deptOptions", "materialAddMethodOptions"],
  components: { ModelMaterial, ProcessParameters },
  data() {
    return {
      setSelectItems: [],
      selectItems: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workingNumber: [
          { required: true, message: "任务编码不能为空", trigger: "blur" },
        ],
        workingName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" },
        ],
        equipmentId: [
          { required: true, message: "设备ID不能为空", trigger: "blur" },
        ],
        deptId: [
          { required: true, message: "公司ID不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "任务描述不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: "任务状态不能为空", trigger: "blur" },
        ],
        electricityPrice: [
          { required: true, message: "平均电价不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 编辑
    openFun(id) {
      this.open = true;
      this.reset();
      this.getInfo(id);
    },
    // 新增
    addFun(){
      this.open = true;
      this.reset();
    },
    getInfo(workingId) {
      getWorking(workingId).then((response) => {
        this.open = true;
        this.$nextTick(() => {
          this.form = response.data;
          this.selectItems = this.form.technologyWorkingParams.map((item) => {
            let obj = JSON.parse(item.paramJson);
            obj.id = item.id;
            return obj;
          });
          this.setSelectItems = this.form.technologyWorkingMaterials; //材料
          this.title = "修改加工任务 ";
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (!this.testForm()) return false;
          if (this.form.workingId != null) {
            updateWorking(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.$emit('changeFun');
            });
          } else {
            addWorking(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.$emit('changeFun');
            });
          }
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        workingId: null,
        workingCategoryId: null,
        workingNumber: null,
        time: [],
        workingName: null,
        equipmentId: null,
        deptId: null,
        description: null,
        startTime: null,
        endTime: null,
        status: 0,
        workingplanId: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isAuto: null,
        processTypeId: null,
        technologyMaterialAddMethodId: null,
        electricityPrice: null,
        spentTime: null,
        technologyWorkingParams: [],
      };
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 验证值
    testForm() {
      let selectItems = this.$refs.parametersRef.selectItems;
      if (selectItems.length == 0) {
        this.msgError("请设置工艺参数");
        return false;
      }
      let setSelectItems = this.$refs.materialRef.setSelectItems;
      if (setSelectItems.length == 0) {
        this.msgError("请选择材料");
        return false;
      }

      let selectItemsRes = selectItems.map((item) => {
        return {
          id: item.id,
          paramJson: JSON.stringify(item),
        };
      });
      this.form.technologyWorkingParams = selectItemsRes;
      this.form.technologyWorkingMaterials = setSelectItems;
      return true;
    },
  },
};
</script>

<style>
</style>